class Employee < ActiveRecord::Base
  has_many :purchase_order
  belongs_to :department
  belongs_to :role
  belongs_to :company

  scope :registered, lambda { |time_ago| { :conditions => ['created_at > ?', time_ago] }}

  #scope :name_like, lambda { |name| { :conditions => ['name like ? OR number like ? OR ab like ?', '%'+name+'%','%'+name+'%','%'+name+'%'],:include=>[:department,:role],:order=>"id,number" }}

  scope :name_like, lambda {  |name,for_cbb|
    if for_cbb == 'false'
      where('name like ? OR number like ? OR ab like ?', '%'+name+'%','%'+name+'%','%'+name+'%').includes([:department,:role])
    else
      where('name like ? OR number like ? OR ab like ?', '%'+name+'%','%'+name+'%','%'+name+'%').select([:id,:name,:number])
    end
  }
  
  scope :paginate, lambda { |start,limit| { :offset => start.to_i,:limit => limit.to_i,:order=>"department_id DESC" }}
end
